
package scripting.reactor;



import client.MapleClient;
 import client.inventory.Equip;
 import client.inventory.Item;
 import client.inventory.MapleInventoryType;
 import constants.ItemConstants;
 import handling.channel.ChannelServer;
 import java.awt.Point;
 import java.util.Collections;
 import java.util.LinkedList;
 import java.util.List;
 import scripting.AbstractPlayerInteraction;
 import server.MapleItemInformationProvider;
 import server.carnival.MapleCarnivalFactory;
 import server.life.MapleLifeFactory;
 import server.life.MapleMonster;
 import server.maps.MapleMapObject;
 import server.maps.MapleReactor;
 import server.maps.ReactorDropEntry;
 import tools.MaplePacketCreator;
 import tools.Randomizer;


 public class ReactorActionManager
         extends AbstractPlayerInteraction {
    /*  26 */   private static final int[] 碎片 = new int[]{4001513, 4001515, 4001521};


       private final MapleReactor reactor;





    public ReactorActionManager(MapleClient c, MapleReactor reactor) {
        /*  34 */
        super(c, reactor.getReactorId(), String.valueOf(c.getPlayer().getMapId()));
        /*  35 */
        this.reactor = reactor;

    }




    public void dropItems() {
        /*  40 */
        dropItems(false, 0, 0, 0, 0, 0, false);

    }







    public void dropItems(int minDrops, int maxDrops) {
        /*  48 */
        dropItems(false, 0, 0, 0, minDrops, maxDrops, true);

    }









    public void dropItems(boolean meso, int mesoChance, int minMeso, int maxMeso) {
        /*  58 */
        dropItems(meso, mesoChance, minMeso, maxMeso, 0, 0, false);

    }













    public void dropItems(boolean meso, int mesoChance, int minMeso, int maxMeso, int minDrops, int maxDrops, boolean profession) {
        /*  72 */
        List<ReactorDropEntry> chances = ReactorScriptManager.getInstance().getDrops(this.reactor.getReactorId());
        /*  73 */
        List<ReactorDropEntry> toDrop = new LinkedList<>();
        /*  74 */
        if (meso && Math.random() < 1.0D / mesoChance) {
            /*  75 */
            toDrop.add(new ReactorDropEntry(0, mesoChance, -1));

        }


        /*  79 */
        for (ReactorDropEntry d : chances) {
            /*  80 */
            if (Math.random() < 1.0D / d.chance && (d.questid <= 0 || getPlayer().getQuestStatus(d.questid) == 1)) {
                /*  81 */
                toDrop.add(d);

            }

        }


        /*  86 */
        while (toDrop.size() < minDrops) {
            /*  87 */
            if (profession && !toDrop.isEmpty()) {
                /*  88 */
                for (int i = 0; i < toDrop.size(); i++) {
                    /*  89 */
                    ReactorDropEntry fix = toDrop.get(i);
                    /*  90 */
                    if (fix != null && toDrop.size() < minDrops)
                        /*  91 */ toDrop.add(fix);

                }

                continue;

            }
            /*  95 */
            toDrop.add(new ReactorDropEntry(0, mesoChance, -1));

        }

        /*  98 */
        if (profession && toDrop.size() > maxDrops) {
            /*  99 */
            toDrop = toDrop.subList(0, maxDrops);

        }
        /* 101 */
        Collections.shuffle(toDrop);
        /* 102 */
        Point dropPos = this.reactor.getPosition();
        /* 103 */
        dropPos.x -= 12 * toDrop.size();

        /* 105 */
        MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
        /* 106 */
        for (ReactorDropEntry de : toDrop) {
            /* 107 */
            if (de.itemId == 0) {
                /* 108 */
                int range = maxMeso - minMeso;
                /* 109 */
                int mesoDrop = Randomizer.nextInt(range) + minMeso * ChannelServer.getInstance(getClient().getChannel()).getMesoRate();
                /* 110 */
                if (mesoDrop > 0) {
                    /* 111 */
                    this.reactor.getMap().spawnMesoDrop(mesoDrop, dropPos, (MapleMapObject) this.reactor, getPlayer(), false, (byte) 0);

                }

            } else {

                Item drop;
                if (ItemConstants.getInventoryType(de.itemId) != MapleInventoryType.EQUIP) {
                    drop = new Item(de.itemId, (byte) 0, (short) 1, (byte) 0);
                } else {
                    drop = ii.randomizeStats((Equip) ii.getEquipById(de.itemId));
                }
                drop.setGMLog("从箱子爆出 " + reactor.getReactorId() + " 在地图 " + getPlayer().getMapId());
                reactor.getMap().spawnItemDrop(reactor, getPlayer(), drop, dropPos, false, false);

            }
            /* 123 */
            dropPos.x += 25;

        }

    }






    public void dropSingleItem(int itemId) {
        Item drop;
        if (ItemConstants.getInventoryType(itemId) != MapleInventoryType.EQUIP) {
            drop = new Item(itemId, (byte) 0, (short) 1, (byte) 0);
        } else {
            drop = MapleItemInformationProvider.getInstance().randomizeStats((Equip) MapleItemInformationProvider.getInstance().getEquipById(itemId));
        }
        drop.setGMLog("采矿(药)掉落 " + reactor.getReactorId() + " 在地图 " + getPlayer().getMapId());
        reactor.getMap().spawnItemDrop(reactor, getPlayer(), drop, reactor.getPosition(), false, false);

    }







    public void spawnNpc(int npcId) {
        /* 146 */
        spawnNpc(npcId, getPosition());

    }








    public Point getPosition() {
        /* 155 */
        Point pos = this.reactor.getPosition();
        /* 156 */
        pos.y -= 10;
        /* 157 */
        return pos;

    }






    public MapleReactor getReactor() {
        /* 164 */
        return this.reactor;

    }




    public void spawnZakum() {
        /* 169 */
        spawnZakum(66000000L);

    }



    public void spawnZakum(long maxhp) {
        /* 173 */
        this.reactor.getMap().spawnZakum((getPosition()).x, (getPosition()).y, maxhp);

    }




    public void spawnChaosZakum() {
        /* 178 */
        spawnChaosZakum(0L);

    }



    public void spawnChaosZakum(long maxhp) {
        /* 182 */
        this.reactor.getMap().spawnChaosZakum((getPosition()).x, (getPosition()).y, maxhp);

    }



    public void spawnSimpleZakum() {
        /* 186 */
        spawnSimpleZakum(0L);

    }



    public void spawnSimpleZakum(long maxhp) {
        /* 190 */
        this.reactor.getMap().spawnSimpleZakum((getPosition()).x, (getPosition()).y, maxhp);

    }






    public void spawnFakeMonster(int id) {
        /* 197 */
        spawnFakeMonster(id, 1, getPosition());

    }










    public void spawnFakeMonster(int id, int x, int y) {
        /* 208 */
        spawnFakeMonster(id, 1, new Point(x, y));

    }









    public void spawnFakeMonster(int id, int qty) {
        /* 218 */
        spawnFakeMonster(id, qty, getPosition());

    }











    public void spawnFakeMonster(int id, int qty, int x, int y) {
        /* 230 */
        spawnFakeMonster(id, qty, new Point(x, y));

    }




    private void spawnFakeMonster(int id, int qty, Point pos) {
        /* 235 */
        for (int i = 0; i < qty; i++) {
            /* 236 */
            this.reactor.getMap().spawnFakeMonsterOnGroundBelow(MapleLifeFactory.getMonster(id), pos);

        }

    }




    public void killAll() {
        /* 242 */
        this.reactor.getMap().killAllMonsters(true);

    }






    public void killMonster(int monsId) {
        /* 249 */
        this.reactor.getMap().killMonster(monsId);

    }









    public void spawnMonster(int id) {
        /* 259 */
        spawnMonster(id, 1, getPosition());

    }










    public void spawnMonster(int id, int qty) {
        /* 270 */
        spawnMonster(id, qty, getPosition());

    }






    public void cancelHarvest(boolean succ) {
        /* 277 */
        getPlayer().setFatigue((byte) (getPlayer().getFatigue() + 1));
        /* 278 */
        getPlayer().getMap().broadcastMessage(getPlayer(), MaplePacketCreator.showHarvesting(getPlayer().getId(), 0), false);
        /* 279 */
        getPlayer().getMap().broadcastMessage(MaplePacketCreator.harvestResult(getPlayer().getId(), succ));

    }




    public void doHarvest() {
        /* 284 */
        if (getPlayer().getFatigue() >= 200 || (getPlayer().getStat()).harvestingTool <= 0 || getReactor().getTruePosition().distanceSq(getPlayer().getTruePosition()) > 10000.0D) {

            return;

        }
        /* 287 */
        int pID = (getReactor().getReactorId() < 200000) ? 92000000 : 92010000;
        /* 288 */
        String pName = (getReactor().getReactorId() < 200000) ? "采药" : "采矿";
        /* 289 */
        int he = getPlayer().getProfessionLevel(pID);
        /* 290 */
        if (he <= 0) {

            return;

        }
        /* 293 */
        Item item = getInventory(1).getItem((short) (getPlayer().getStat()).harvestingTool);
        /* 294 */
        if (item == null || item.getItemId() / 10000 != ((getReactor().getReactorId() < 200000) ? 150 : 151)) {

            return;

        }
        /* 297 */
        int hm = getReactor().getReactorId() % 100;
        /* 298 */
        int successChance = 90 + (he - hm) * 10;
        /* 299 */
        if (getReactor().getReactorId() % 100 == 10) {
            /* 300 */
            hm = 1;
            /* 301 */
            successChance = 100;
            /* 302 */
        } else if (getReactor().getReactorId() % 100 == 11) {
            /* 303 */
            hm = 10;
            /* 304 */
            successChance += 20;

        }
        /* 306 */
        getPlayer().getStat().checkEquipDurabilitys(getPlayer(), -1, true);
        /* 307 */
        int masteryIncrease = (hm - he) * 2 + 20;
        /* 308 */
        boolean succ = (randInt(100) < successChance);
        /* 309 */
        if (!succ) {
            /* 310 */
            masteryIncrease /= 10;

        } else {

            /* 313 */
            int minDrops = 1;
            /* 314 */
            int maxDrops = (getReactor().getReactorId() % 100 == 11) ? 6 : 2;
            /* 315 */
            if (item.getItemId() == 1512001) {
                /* 316 */
                minDrops += 2;
                /* 317 */
                maxDrops += 2;
                /* 318 */
            } else if (item.getItemId() == 1512004 || item.getItemId() == 1512005) {
                /* 319 */
                minDrops += 3;
                /* 320 */
                maxDrops += 3;
                /* 321 */
            } else if (item.getItemId() == 1512006) {
                /* 322 */
                minDrops += 5;
                /* 323 */
                maxDrops += 5;

            }
            /* 325 */
            dropItems(minDrops, maxDrops);
            /* 326 */
            if (getReactor().getReactorId() < 200000) {
                /* 327 */
                addTrait("sense", 5);
                /* 328 */
                if (Randomizer.nextInt(10) <= 1) {
                    /* 329 */
                    dropSingleItem(2440000);

                }
                /* 331 */
                if (Randomizer.nextInt(100) == 0) {
                    /* 332 */
                    dropSingleItem(4032933);

                }
                /* 334 */
                if (Randomizer.nextInt(10) <= 1) {
                    /* 335 */
                    dropSingleItem(碎片[Randomizer.nextInt(碎片.length)]);

                }

            } else {
                /* 338 */
                addTrait("insight", 5);
                /* 339 */
                if (Randomizer.nextInt(10) <= 1) {
                    /* 340 */
                    dropSingleItem(2440001);

                }
                /* 342 */
                if (Randomizer.nextInt(10) <= 1) {
                    /* 343 */
                    dropSingleItem(碎片[Randomizer.nextInt(碎片.length)]);

                }

            }

        }
        /* 347 */
        cancelHarvest(succ);
        /* 348 */
        playerMessage(-5, pName + "的熟练度提高了。(+" + masteryIncrease + ")");
        /* 349 */
        if (getPlayer().addProfessionExp(pID, masteryIncrease)) {
            /* 350 */
            playerMessage(-5, pName + "的等级提升了。");

        }

    }



    public void delayedDestroyReactor(long time) {
        /* 355 */
        this.reactor.delayedDestroyReactor(time);

    }



    public void dispelAllMonsters(int num) {
        /* 359 */
        MapleCarnivalFactory.MCSkill guardian = MapleCarnivalFactory.getInstance().getGuardian(num);
        /* 360 */
        if (guardian != null) {
            /* 361 */
            for (MapleMonster monster : getMap().getAllMonstersThreadsafe()) {
                /* 362 */
                monster.dispelSkill(guardian.getSkill());

            }

        }

    }



    public void setSrcPos() {
        /* 368 */
        getPlayer().setReactor(null);
        /* 369 */
        this.reactor.setPosition(this.reactor.getSrcPos());

    }

}


/* Location:              C:\PGX\Server.jar!\scripting\reactor\ReactorActionManager.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */